package net.dgg.dggcloud.service.jrcrm.service.cms;

import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.jrcrm.dao.cms.RuleConfigDao;
import net.dgg.dggcloud.service.jrcrm.entity.cms.RuleConfig;
import net.dgg.dggcloud.service.entity.user.User;
import org.apache.batik.css.engine.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: RuleConfigService
 * @Description: 规则配置逻辑
 * @author 汤宏
 * @date 2017年11月13日11:00:00
 *
 */
@Service
public class RuleConfigService extends BaseService {

    @Autowired
    private RuleConfigDao ruleConfigDao;


    /**
     * 查询规则配置
     * @param map organizationId 事业部ID
     * @return
     */
    public List<Map> listRuleConfigByMap(Map map){
        return ruleConfigDao.listRuleConfigByMap(map);
    }
    /**
     * 保存规则设置
     * @param val 规则值
     * @param id 规则ID
     * @param loger 登录人
     * @return
     */
    public void saveRuleConfig(String[] val,Long[] id,User loger){
        for (int i = 0; i < id.length; i++) {
            RuleConfig ruleConfig = super.repository.get(RuleConfig.class,id[i]);
            ruleConfig.setVal(val[i]);
            ruleConfig.setUpdaterUser(loger);
            super.repository.save(ruleConfig);
        }
    }
    /**
     * 更新规则状态
     * @param id 规则ID
     * @param status 状态
     * @param loger 登录人
     * @return
     */
    public void updateRuleConfigStatus(Long id,Integer status,User loger){
            RuleConfig ruleConfig = super.repository.get(RuleConfig.class,id);
            ruleConfig.setStatus(status);
            ruleConfig.setUpdaterUser(loger);
            super.repository.save(ruleConfig);
    }
    /**
     * 查询规则参数（单个）
     * @param  organizationId 事业部ID
     *            code 规则编码
     *
     * @return
     */
    public String getRuleConfigByMap(Long organizationId,String code){
        Map map = new HashMap<>();
        map.put("organizationId",organizationId);
        map.put("code",code);
        return ruleConfigDao.getRuleConfigByMap(map);
    }
    /**
     * 查询规则参数（多个）
     * @param  organizationId 事业部ID
     *            type 分类编码
     *
     * @return
     */
    public List<Map> getRuleConfigsByMap(Long organizationId,String code){
        Map map = new HashMap<>();
        map.put("organizationId",organizationId);
        map.put("code",code);
        return ruleConfigDao.getRuleConfigsByMap(map);
    }


    /**
     * 查询规则参数（多个）
     * @param  organizationId 事业部ID
     *            type 分类编码
     *
     * @return
     */
    public List<Map> getRuleConfigsByMaps(Long organizationId,String code){
        Map map = new HashMap<>();
        map.put("organizationId",organizationId);
        map.put("type",code);
        return ruleConfigDao.getRuleConfigsByMap(map);
    }
}
